<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
</head>
<body>
<form>
<table>
    <tr>
        <td>
            用户名
        </td>
        <td>
            <input id="username">
        </td>
    </tr>
    <tr>
        <td>
            密码
        </td>
        <td>
            <input id="password">
        </td>
    </tr>
    <tr>
        <td>
            确认密码
        </td>
        <td>
            <input id="confirm">
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
            <input onclick="register()" type="button" value="确认">
        </td>
    </tr>
</table>
</form>
</body>
<script>
    function register() {
        var username = document.getElementById("username").value;
        var password = document.getElementById("password").value;
        var confirm = document.getElementById("confirm").value;
        if (!username || !password || !confirm) {
            alert("用户名，密码，确认密码不能为空");
            return;
        }
        if (password !== confirm) {
            alert("两次密码不相同");
            return;
        }
        Ajax.post("/users/register", {
            username: username,
            password: password,
        }, function (data) {
            console.log(data)
        });
    }
    var Ajax = {
        post: function (url, data, fn) {
            var obj = new XMLHttpRequest();
            obj.open("POST", url, true);
            obj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            obj.onreadystatechange = function () {
                fn.call(this, obj.responseText);
            };
            var sendData = "";
            for (var key in data) {
                sendData += key + "=" + data[key] + "&";
            }
            if (sendData !== "") {
                sendData = sendData.substr(0, sendData.length - 1);
            }
            obj.send(sendData);
        }
    }
</script>
</html>